/*
 * PDFPreviewWindow.java
 *
 *  This file is part of the PDFGen Syllabus Formatter Project.
 *  Copyright (C) 2011
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dlsu.pdfgen.gui;

import java.io.IOException;

/**
 * Re-implementation of PDF Preview based on
 * freebats5-10724613 (Vitug)
 * 
 * @author Marwin Terence Lao -- OPENSRC 3rd Term AY 2010-2011
 */
public class PDFPreviewWindow extends javax.swing.JFrame {

    com.sun.pdfview.PDFFile file;
    int pageNum = 1;
    int totalPages = 0;
    java.awt.Image image;
    java.awt.Rectangle rect;
    
    /** Creates new form PDFPreviewWindow */
    public PDFPreviewWindow(String source) 
            throws java.io.FileNotFoundException, IOException {
        initComponents();
        try{
            loadPDF(source);

            totalPages = file.getNumPages();
            pageLabel.setText(""+totalPages);

            pageField.setText("" + pageNum);
            com.sun.pdfview.PDFPage page = file.getPage(pageNum);

            setPagetoImage();
        }
        catch(Exception x){}
	setVisible(true);
	setLocationRelativeTo(null);
    }

    private void loadPDF(String source) throws 
            java.io.FileNotFoundException, java.io.IOException {
        java.nio.channels.FileChannel fc 
                = new java.io.RandomAccessFile(new java.io.File(source), "r")
                .getChannel();
        java.nio.ByteBuffer buf = fc.map
                (java.nio.channels.FileChannel.MapMode.READ_ONLY, 0, fc.size());
        file = new com.sun.pdfview.PDFFile(buf);
    }

    /** 
     * Sets the current page to be displayed.
     * 
     * 
     */
    private void setPagetoImage() {
        com.sun.pdfview.PDFPage page = file.getPage(pageNum);

        rect = new java.awt.Rectangle(0,0,
            (int)page.getBBox().getWidth(),
            (int)page.getBBox().getHeight());

        image = page.getImage(rect.width , rect.height, rect, null, true, true);

        previewPane.setIcon(new javax.swing.ImageIcon(image));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        prevButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pageField = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        pageLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        goToButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        previewPane = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.dlsu.pdfgen.Controller.title+" - Preview");
        setMinimumSize(new java.awt.Dimension(640, 480));

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        prevButton.setText("◄");
        prevButton.setFocusable(false);
        prevButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        prevButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(prevButton);

        nextButton.setText("►");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(nextButton);
        jToolBar1.add(jSeparator1);

        jPanel1.setEnabled(false);
        jPanel1.setPreferredSize(new java.awt.Dimension(100, 25));
        jPanel1.setLayout(new java.awt.BorderLayout());
        jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

        pageField.setColumns(4);
        pageField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        pageField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        jPanel1.add(pageField, java.awt.BorderLayout.LINE_END);

        jToolBar1.add(jPanel1);

        jLabel2.setText(" / ");
        jToolBar1.add(jLabel2);

        pageLabel.setText("0");
        jToolBar1.add(pageLabel);

        jLabel3.setText("   ");
        jToolBar1.add(jLabel3);

        goToButton.setText("Go to");
        goToButton.setFocusable(false);
        goToButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        goToButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        goToButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(goToButton);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setViewportView(previewPane);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevButtonActionPerformed
        if (pageNum > 1) {
            pageNum--;
            pageField.setText("" + pageNum);
            setPagetoImage();
        }
    }//GEN-LAST:event_prevButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        if (pageNum < totalPages) {
            pageNum++;
            pageField.setText("" + pageNum);
            setPagetoImage();
        }
    }//GEN-LAST:event_nextButtonActionPerformed

    private void goToButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToButtonActionPerformed
        pageNum = Integer.parseInt(pageField.getText());
        setPagetoImage();
    }//GEN-LAST:event_goToButtonActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton goToButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton nextButton;
    private javax.swing.JFormattedTextField pageField;
    private javax.swing.JLabel pageLabel;
    private javax.swing.JButton prevButton;
    private javax.swing.JLabel previewPane;
    // End of variables declaration//GEN-END:variables
}
